<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>待审批请假</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/resources/css/public.css" media="all">
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend style="color: #35c6d3;font-weight:bold;">待审批请假列表</legend>
</fieldset>
<div class="layuimini-container" style="padding-left:50px;padding-right: 50px;">
    <div class="layuimini-main">
        <!-- 数据表格开始 -->
        <table class="layui-hide" id="leaveTable" lay-filter="leaveTableFilter"></table>
        <div id="currentTableBar" style="display: none;">
            <button type="button" class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="agree">同意
            </button>
            <button type="button" class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="refuse">
                拒绝
            </button>
        </div>

        <!-- 数据表格结束 -->

        <!-- 拒绝的弹出层开始 -->
        <div style="display: none;margin-right: 20px;" class="layui-form layuimini-form" id="agreeDiv">
            <form action="" method="post" class="layui-form" id="dataFrm" lay-filter="dataFrm">
                <div class="layui-form-item"></div>
                <div class="layui-form-item">
                    <input type="hidden" name="id">
                    <label class="layui-form-label">拒绝原因</label>
                    <div class="layui-input-block">
                        <textarea name="refusereason" lay-verify="required" lay-reqtext="拒绝原因不能为空!"  class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="doSubmit">确认保存</button>
                        <button type="reset" class="layui-btn layui-btn-normal"><span
                                class="layui-icon layui-icon-refresh-1"></span>重置
                        </button>
                    </div>
                </div>
            </form>
        </div>
        <!-- 同意和拒绝的弹出层结束 -->


    </div>
</div>

<script type="text/javascript" src="/resources/layui/layui.js" charset="utf-8"></script>

<script type="text/javascript">
    layui.use(['jquery', 'form', 'table', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        //加载数据
        var tableIns = table.render({
            elem: '#leaveTable',
            url: '/leave/loadAllLeaveByStatus?status=0',
            // toolbar: '#toolbarDemo',
            cols: [
                [
                    {field: 'id', title: 'ID', sort: true},
                    {field: 'sid', title: '学号', align: 'center'},
                    {field: 'studentname', title: '姓名', align: 'center'},
                    {field: 'grade', title: '级别', align: 'center'},
                    {field: 'classes', title: '班级', align: "center"},
                    {field: 'createtime', title: '请假时间', align: "center"},
                    {
                        field: 'status', title: '状态', align: 'center', templet: function (data) {
                            if (data.status == 0) {
                                return '<span class="ebe-badge ebe-tag-danger">' + '待审批';
                            } else if (data.status == 1) {
                                return '<span class="ebe-badge ebe-tag-warning">' + '请假失败';
                            } else if (data.score == 2) {
                                return '<span class="ebe-badge ebe-tag-success">' + '未销假';
                            } else {
                                return '<span class="ebe-badge ebe-tag-success">' + '已销假';
                            }

                        }
                    },
                    {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            done: function (res, curr, count) { //处理删除某一页最后一条数据的BUG
                if (res.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: (curr - 1)
                        }
                    });
                }
            }
        });

        //监听行工具条的事件
        table.on("tool(leaveTableFilter)", function (obj) {
            var data = obj.data; //获得当前行数据
            switch (obj.event) {
                case 'agree':
                    agreeLeave(data);
                    break;
                case 'refuse':
                    openRefuseLeaveLayer(data);
                    break;
            }
            ;
        });

        //同意
        function agreeLeave(data) {
            layer.confirm('你确定要同意【' + data.studentname + '】的假单吗?', {icon: 3, title: '提示'}, function (index) {
                $.post("/leave/agreeLeave", {id: data.id}, function (res) {
                    if (res.code == 200) {
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            });
        }

        var mainIndex;
        var url;

        //打开拒绝的弹出层
        function openRefuseLeaveLayer(data) {
            mainIndex = layer.open({
                type: 1,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                content: $("#agreeDiv"),
                area: ['800px', '600px'],
                title: '拒绝假单',
                success: function () {
                    $("#dataFrm")[0].reset();
                    //装载新的数据
                    form.val("dataFrm",data);
                    url = "/leave/updateLeave";
                }
            });
        }

        form.on("submit(doSubmit)",function(data){
            $.post(url,data.field,function(res){
                if(res.code==200){
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            })
            return false;
        });
    });
</script>

</body>
</html>